package org.example.likou150.array;

import java.util.Arrays;

public class MinSubArrayLen {
    public static void main(String[] args) {
        int[] nums=new int[]{1,2,3,4,5};
        MinSubArrayLen minSubArrayLen=new MinSubArrayLen();
        int i = minSubArrayLen.minSubArrayLen(11, nums);
        System.out.println(i);
    }

    public int minSubArrayLen(int target, int[] nums) {
        int left=0;
        int right=nums.length-1;
        int count=0;
        int total=0;
        for (int i = 0; i < nums.length; i++) {
            total+=nums[i];
        }
        if (total<target){
            return count;
        }
        while(total>=target){
            if(left==right){
                return 1;
            }
            if(nums[left]<nums[right]){
                total-=nums[left];
                if(total<target){
                    return count;
                }else{
                    count++;
                    left++;
                }
            }else {
                total-=nums[right];
                if(total<target){
                    return count;
                }else{
                    count++;
                    right--;
                }
            }
        }
        return right-left;
    }
}
